#include <stdio.h>
#include "sdkconfig.h"

#include "esp_heap_caps.h"
#include "esp_spi_flash.h"
#include "esp_system.h"

#include "lvgl_port.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "audio.h"
#include "nvs_flash.h"
#include "esp_spiffs.h"
#include "i2c_bus2.h"
#include "music.h"
#include "gpio.h"
#include "color.h"
#include "main.h"
#include "storage.h"
#include "wifi.h"
#include "wifi_scan.h"
#include "wifi_scan_page.h"
#include "main_page.h"
#include "lv_symbol_extra_def.h"
#include "weather_page.h"
#include "lvgl_port.h"
#include "lv_port_fs.h"
#include "music_page.h"
#include "task_mag.h"
#include "pwm.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
#include "cJSON.h"
#include "strings.h"
#include "http_time.h"
#include "udp.h"
#include "console.h"
#include "version.h"
#include "terminal_page.h"

#define TAG "CONSOLE"

extern xQueueHandle terminal_page_queue;
extern char *console_str2;

#define CONSOLE_CMD_NUM 7

char cmd_list[CONSOLE_CMD_NUM][16] = {
    "clean",
    "version",
    "ipconfig",
    "idf version",
    "reset",
    "help",
    "heap",
};

/**
 * @brief 终端指令处理
 *
 * @param cmd 指令字符串
 */
void console_handle(char *cmd)
{
    uint32_t page_option_data;
    char *offset;
    char str[64];

    if (!strlen(cmd))
        return;

    if (!console_str2)
        return;

    // clean
    offset = strstr(cmd, &cmd_list[0]);
    if (offset && offset == cmd)
    {
        page_option_data = PAGE_CLEAN_CONSOLE;
        xQueueSend(terminal_page_queue, &page_option_data, NULL);
        goto en;
    }

    // version
    offset = strstr(cmd, &cmd_list[1]);
    if (offset && offset == cmd)
    {
        sprintf(str, "%s %s", DEVICE_NAME, SV_VERSION);
        console_add_lin(console_str2, str);
        goto en;
    }

    // ipconfig
    offset = strstr(cmd, &cmd_list[2]);
    if (offset && offset == cmd)
    {
        if (get_ip_str())
        {
            sprintf(str, "ip: %s", get_ip_str());
            console_add_lin(console_str2, str);
            goto en;
        }
    }

    // idf version
    offset = strstr(cmd, &cmd_list[3]);
    if (offset && offset == cmd)
    {
        sprintf(str, "%.*s", 10, esp_get_idf_version());
        console_add_lin(console_str2, str);

        sprintf(str, "%s", esp_get_idf_version() + 10);
        console_add_lin(console_str2, str);
        goto en;
    }

    // reset
    offset = strstr(cmd, &cmd_list[4]);
    if (offset && offset == cmd)
    {
        page_option_data = PAGE_CLEAN_CONSOLE_INPUT;
        xQueueSend(terminal_page_queue, &page_option_data, NULL);

        console_add_lin(console_str2, "Restarting now.");
        vTaskDelay(1000 / portTICK_RATE_MS);
        fflush(stdout);
        esp_restart();
    }

    // help
    offset = strstr(cmd, &cmd_list[5]);
    if (offset && offset == cmd)
    {
        sprintf(str, "command list (%d):", CONSOLE_CMD_NUM);
        console_add_lin(console_str2, str);
        vTaskDelay(100 / portTICK_RATE_MS);

        for (size_t i = 0; i < CONSOLE_CMD_NUM; i++)
        {
            sprintf(str, "%d. %s", i + 1, cmd_list[i]);
            console_add_lin(console_str2, str);
            vTaskDelay(100 / portTICK_RATE_MS);
        }
    }

    // heap
    offset = strstr(cmd, &cmd_list[6]);
    if (offset && offset == cmd)
    {
        sprintf(str, "heap %d byte.", esp_get_free_heap_size());
        console_add_lin(console_str2, str);
        goto en;
    }

en:
    /**
     * @brief 清空输入框
     *
     */
    page_option_data = PAGE_CLEAN_CONSOLE_INPUT;
    xQueueSend(terminal_page_queue, &page_option_data, NULL);
    return;
}
